package com.lyEducation.controllers.dto;

import com.lyEducation.core.base.ResponseCodeConstans;
import com.lyEducation.core.base.ValidationUtils;
import com.lyEducation.util.StringUtils;
import com.lyEducation.util.controllerUtil.JsonResult;
import lombok.Getter;
import lombok.Setter;

import java.util.Objects;

@Setter
@Getter
public abstract class BaseDto extends AbstractDto {
  private String name;

  protected <T extends BaseDto> JsonResult validationEntity(T dto) {
    String isVali = ValidationUtils.validation(dto);
    if (!isVali.equals(ResponseCodeConstans.SUCCESS)) {
      return JsonResult.putFail(isVali);
    }
    return null;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof BaseDto)) return false;
    if (!super.equals(o)) return false;
    BaseDto baseDto = (BaseDto) o;
    return Objects.equals(name, baseDto.name);
  }

  @Override
  public int hashCode() {

    return Objects.hash(super.hashCode(), name);
  }

  //  protected boolean paramIDNameCheck(AbstractItem abstractItem) {
  //    if (!paramIdCheck(abstractItem.getId())) {
  //      return false;
  //    }
  //    return paramNameCheck(abstractItem.getName());
  //  }
  public JsonResult checkNumber(String errorParamName, String numberStr) {
    if (StringUtils.isBlank(numberStr)) {
      return JsonResult.putFail(errorParamName + "不能为空");
    }
    if (!StringUtils.isNumeric(numberStr)) {
      return JsonResult.putFail(errorParamName + "必须为大于1的正整数");
    }
    return null;
  }

  public JsonResult checkName(String errorParamName) {
    return checkName(errorParamName, this.name, 255);
  }

  public JsonResult checkName(String errorParamName, String param, Integer length) {
    if (StringUtils.isBlank(param)) {
      return JsonResult.putFail(errorParamName + "不能为空");
    }
    if (length != null) {
      if (param.length() > length) {
        return JsonResult.putFail(errorParamName + "长度超过限制,长度不能超过" + length + "字符");
      }
    }
    return null;
  }
}
